package com.example.demo.service;

import com.example.demo.domin.entity.DepartMent;
import com.example.demo.domin.entity.Role;
import com.example.demo.domin.entity.User;
import com.example.demo.domin.repository.DepartMentRepository;
import com.example.demo.domin.repository.RoleRepository;
import com.example.demo.domin.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.annotation.AccessType;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

/**
 * Created by kaizige on 2017/7/10.
 */
@Service
public class UserService {
    private static Logger logger= LoggerFactory.getLogger(UserService.class);
    @Autowired
    UserRepository userRepository;
    @Autowired
    DepartMentRepository departMentRepository;
    @Autowired
    RoleRepository roleRepository;

    @CachePut(value = "mycache",key="'id:'+#p0.id")
    public boolean save(User user){
        System.out.println("save=======");
        userRepository.save(user);
        return true;
    }
    @CachePut(value = "mycache",key="'id:'+#p0")
    public boolean update(User user){
        System.out.println("update=======");
        userRepository.save(user);
        return true;
    }
    @CacheEvict(value = "mycache",allEntries = true)
    public boolean delete(User user){
        System.out.println("delete=======");
        userRepository.delete(user);
        return true;
    }
    @Cacheable(value = "mycache",key="#id+\"\"")
    public User findById(Long  id){
       logger.info("insert================");
        return userRepository.findOne(id);

    }

}
